package com.mdp.workflow.re.ctrl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.entity.Result;
import com.mdp.core.err.BizException;
import com.mdp.core.query.QueryTools;
import com.mdp.safe.client.entity.User;
import com.mdp.safe.client.utils.LoginUtils;
import com.mdp.swagger.ApiEntityParams;
import com.mdp.workflow.re.entity.ProcdefTag;
import com.mdp.workflow.re.service.ProcdefTagService;
import com.mdp.workflow.re.vo.ProcdefTagVo;
import com.mdp.workflow.re.service.ProcdefParamesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value="/**/re/procdefTag")
@Api(tags={"会员标签登记表-操作接口"})
public class ProcdefTagController {

	static Logger logger = LoggerFactory.getLogger(ProcdefTagController.class);

	@Autowired
	private ProcdefTagService procdefTagService;

	@Autowired
	private ProcdefParamesService procdefParamesService;

	@ApiOperation(value = "会员标签登记表-查询列表", notes = " ")
	@ApiEntityParams(ProcdefTag.class)
	@ApiResponses({
			@ApiResponse(code = 200, response = ProcdefTag.class, message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'错误码'},total:总记录数,data:[数据对象1,数据对象2,...]}")
	})
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result listProcdefTag(@ApiIgnore @RequestParam Map<String, Object> params) {
		try {
			User user = LoginUtils.getCurrentUserInfo();
			QueryWrapper<ProcdefTag> qw = QueryTools.initQueryWrapper(ProcdefTag.class, params);
			IPage page = QueryTools.initPage(params);
			List<Map<String, Object>> datas = procdefTagService.selectListMapByWhere(page, qw, params);
			return Result.ok("query-ok", "查询成功").setData(datas).setTotal(page.getTotal());
		} catch (BizException e) {
			return Result.error(e);
		} catch (Exception e) {
			return Result.error(e);
		}
	}

	/**
	 * 批量打标签
	 * [
	 * {procDefId:'':'',branchId:'',userid:'',tags:[
	 * {tagId:'',tagName:''}
	 * ]
	 * }
	 * ]
	 *
	 * @param tagsVos
	 * @return
	 */
	@RequestMapping(value = "/batchInsertOrDeleteTags", method = RequestMethod.POST)
	public Result batchDelProcdefTag(@RequestBody List<ProcdefTagVo> tagsVos) {

		if (tagsVos == null) {
			return Result.error("请上送参数列表");
		} else if (tagsVos.size() > 10) {
			return Result.error("一次只能同时给十个流程打标签");
		} else {
			for (ProcdefTagVo tagsVo : tagsVos) {
				List<com.mdp.workflow.re.entity.ProcdefTag> tags = tagsVo.getTags();
				String procDefId = tagsVo.getProcDefId();
				String branchId = tagsVo.getBranchId();
				String userid = tagsVo.getUserid();
				String tagIds = "";
				String tagNames = "";
				if (tags != null && tags.size() > 0) {
					for (com.mdp.workflow.re.entity.ProcdefTag tag : tags) {
						tagIds = tagIds + "," + tag.getTagId();
						tagNames = tagNames + "," + tag.getTagName();
					}
					tagIds = tagIds.substring(1);
					tagNames = tagNames.substring(1);
				}
				procdefParamesService.updateTagsByProDefId(branchId, procDefId, tagIds, tagNames);
				procdefTagService.batchInsertOrDeleteTags(branchId, procDefId, userid, tags);
			}
			return Result.ok();
		}

	}
}
